package com.lagou.controller;

import com.github.pagehelper.PageInfo;
import com.lagou.domain.PromotionAd;
import com.lagou.domain.PromotionAdVO;
import com.lagou.domain.ResponseResult;
import com.lagou.service.PromotionAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/PromotionAd")
public class PromotionAdController {

    @Autowired
    private PromotionAdService promotionAdService;

    /**
     * 分页查询所有广告信息
     *
     * @param promotionAdVO
     * @return
     */
    @RequestMapping("/findAllPromotionAdByPage")
    public ResponseResult findAllPromotionAd(PromotionAdVO promotionAdVO) {

        PageInfo<PromotionAd> pageInfo = promotionAdService.findAllAdByPage(promotionAdVO);
        return new ResponseResult(true, 200, "分页查询成功", pageInfo);
    }

    /**
     * 图片上传接口
     *
     * @param file
     * @param request
     * @return
     */
    @RequestMapping("/PromotionAdUpload")
    public ResponseResult fileUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {

        if (file.isEmpty()) {

            throw new RuntimeException();
        }

        String realPath = request.getServletContext().getRealPath("/");
        String webappsPath = realPath.substring(0, realPath.indexOf("ssm-web"));
        String fileName = file.getOriginalFilename();
        String newFileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));
        String uploadPath = webappsPath + "upload\\";
        File file1 = new File(uploadPath, newFileName);
        if (!file1.getParentFile().exists()) {

            file1.getParentFile().mkdirs();
            System.out.println("创建目录：" + file1);
        }

        try {
            file.transferTo(file1);
            Map<String, String> map = new HashMap<>();
            map.put("fileName", newFileName);
            map.put("filePath", "http:localhost:8080/upload/" + newFileName);
            return new ResponseResult(true, 200, "图片上传成功", map);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 修改广告上下线状态
     *
     * @param id     广告id
     * @param status 广告状态
     * @return
     */
    @RequestMapping("/updatePromotionAdStatus")
    public ResponseResult updatePromotionAdStatus(Integer id, Integer status) {

        promotionAdService.updatePromotionAdStatus(id, status);
        Map<String, Integer> map = new HashMap<>();
        map.put("status", status);
        return new ResponseResult(true, 200, "修改广告上下线状态", map);
    }

    /**
     * 修改回显广告信息接口
     *
     * @param id 广告id
     * @return
     */
    @RequestMapping("/findPromotionAdById")
    public ResponseResult findPromotionAdById(Integer id) {

        PromotionAd promotionAd = promotionAdService.findPromotionAdById(id);
        return new ResponseResult(true, 200, "获取广告成功", promotionAd);
    }

    /**
     * 新建&修改广告接口
     *
     * @param promotionAd
     * @return
     */
    @RequestMapping("/saveOrUpdatePromotionAd")
    public ResponseResult saveOrUpdatePromotionAd(@RequestBody PromotionAd promotionAd) {

        String msg = null;
        if (promotionAd.getId() == null) {

            promotionAdService.savePromotionAd(promotionAd);
            msg = "新建广告";
        } else {

            promotionAdService.updatePromotionAd(promotionAd);
            msg = "修改广告";
        }

        return new ResponseResult(true, 200, msg, null);
    }
}
